const mongoose = require('mongoose')

//连接数据库 
mongoose.connect('mongodb://username:password@localhost:27017/schoolRun?authSource=admin')
    .then(res => {
        //console.log(res)
        console.log('数据库连接成功')
    })
    .catch(err => {
        //console.log(err)
        console.log('数据库连接失败')
    })

// mongoose.set('debug', true)
//定义用户表规则
const userInfoSchema = new mongoose.Schema({
    openid: {
        type: String,
        unique: true
    },
    nickName: String,
    avatarUrl: String,
    gender: {
        type: String,
        default: '暂不设置'
    },
    phone: String,
    canReceive: {
        type: Boolean,
        default: false
    },
    personInfo: {
        type: Object,
        default: null
    },
    email: String
})

//model
const user = mongoose.model('UserInfo', userInfoSchema, 'userInfo')

// module.exports = {
//     user
// }
//建立用户地址信息表 
const addressSchema = new mongoose.Schema({
    openid: {
        type: String,
        required: true
    },
    building: String,//楼栋 
    doorNum: String,//门牌号 
    name: String,//收件姓名 
    phone: String,//收件电话 
    isDefault: {//默认地址 
        type: Boolean,
        defalut: false
    },
})
//创建地址model对象 
const address = mongoose.model('Address', addressSchema, 'address')
//建立管理员信息表======================================================== 
const adminSchema = new mongoose.Schema({ 
    openid: String, 
    username: String, 
    password: String, 
    nickName: String, 
    role: String, 
    time: String
 })
const admin = mongoose.model('Admin', adminSchema, 'admin');
//  第一次运行时将下面注释打开，创建一个管理员用户，openid要改成自己的
//  admin.create({ 
//  openid : 'oa7me7Yqwmce6xFZHOtbzVfyhfH0', 
//  username : 'admin',
//  password : '123456', 
//  role : '超级管理员', 
//  nickName : '王勤磊', 
//  time : '2024-12-31 23:59:59' 
//  }) 
//建立订单信息表 
const orderSchema = new mongoose.Schema({
    openid: String,
    startAddress: { type: String, default: '' },
    endAddress: { type: String, default: '' },
    orderType: String,
    payMoney: Number,
    publicTime: String,
    status: String,
    info: Object,
    userInfo: Object,
    acceptMan: { type: String, default: '' },
    acceptTime: { type: Date, default: null },
    confirmTime: { type: Date, default: null }
})
//创建地址model对象 
const order = mongoose.model('Order', orderSchema, 'order')
//导出 
module.exports = {
    user,
    address,
    admin,
    order,
}

